#include <flutter/dart_project.h>
#include <flutter/flutter_view_controller.h>
#include <windows.h>

#include "flutter_window.h"
#include "utils.h"

int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
                      _In_ wchar_t *command_line, _In_ int show_command) {
  // Attach to console when present (e.g., 'flutter run') or create a
  // new console when running with a debugger.
  if (!::AttachConsole(ATTACH_PARENT_PROCESS) && ::IsDebuggerPresent()) {
    CreateAndAttachConsole();
  }

  // Initialize COM, so that it is available for use in the library and/or
  // plugins.
  ::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);

  flutter::DartProject project(L"data");

  std::vector<std::string> command_line_arguments =
      GetCommandLineArguments();
  // command_line_arguments.push_back("--enable-transparent-background");

  project.set_dart_entrypoint_arguments(std::move(command_line_arguments));


  int h = GetSystemMetrics(SM_CYVIRTUALSCREEN);

  FlutterWindow window(project);
  Win32Window::Point origin(10, h);
  Win32Window::Size size(260, 260);
  if (!window.Create(L"tools", origin, size)) {
    return EXIT_FAILURE;
  }

  HWND hwnd = window.GetNativeWindow();
  // 扩展样式：分层 + 工具窗口（隐藏任务栏图标）
  LONG exStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
  exStyle |= WS_EX_LAYERED | WS_EX_TOOLWINDOW;   // 添加工具窗口
  exStyle &= ~WS_EX_APPWINDOW;                   // 移除应用窗口样式（任务栏图标）
  SetWindowLong(hwnd, GWL_EXSTYLE, exStyle);
  SetLayeredWindowAttributes(hwnd, 0, 255, LWA_COLORKEY);

    // 移除窗口边框
  LONG style = GetWindowLong(hwnd, GWL_STYLE);
  style &= ~WS_OVERLAPPEDWINDOW; // 移除常规窗口样式（含边框、标题栏等）
  style |= WS_POPUP;             // 添加无边框样式
  SetWindowLong(hwnd, GWL_STYLE, style);

  // 强制窗口样式立即生效
  SetWindowPos(hwnd, nullptr, 0, 0, 0, 0,
               SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);


  //HRGN hrgn = CreateEllipticRgn(0, 0, 260, 260);
  //SetWindowRgn(hwnd, hrgn, TRUE);

  ShowWindow(hwnd, SW_HIDE);



  window.SetQuitOnClose(true);

  ::MSG msg;
  while (::GetMessage(&msg, nullptr, 0, 0)) {
    ::TranslateMessage(&msg);
    ::DispatchMessage(&msg);
  }

  ::CoUninitialize();
  return EXIT_SUCCESS;
}
